import { http } from ".";
import type { ArticleInter, ArticleTagInter } from "../types/article";
import type { ResultInter } from "../types/systemInstance";


// ----------------------------------------------------------------------------------------visitor-------------------------------------------------------------------------
// 完整的文章列表
export async function getArticleList() {

    try {
        let data = await http.get("/articles/visitor/articleMetaList");
        return data.data.list //直接返回文章的list列表
    } catch (error) {
        throw new Error(`获取文章list失败: ${error}`)
    }
}

// 获取文章内容
export async function getArticleContentById(id: number) {

    try {
        let data = await http.get(`/articles/visitor/content/${id}`);
        return data.data.content; //直接返回文章内容
    } catch (error) {
        throw new Error(`获取文章list失败: ${error}`)
    }

}

// ---------------------------------------------------------------------------------------------user-------------------------------------------------------------------------

// 得到用户管理页面文章列表
export async function getUserArticleList() {
    try {
        let data = await http.get("/articles/user/articleMetaList")
        return data.data.list;
    } catch (error) {
        throw new Error(`用户文章列表获取失败: ${error}`)
    }
}

//  用户发布文章接口
export async function createArticle(article: ArticleInter) {
    try {
        let data = await http.post("/articles/user/articleMeta", article) as ResultInter;

        return data; //

    } catch (error) {
        throw new Error(`创建文章失败: ${error}`)
    }
}

// 删除文章的方法 这个方法只需要传递id即可
export async function deleteArticleById(id: number) {
    try {
        let data = await http.delete(`/articles/user/articleMeta/${id}`) as ResultInter;
        return data;
    } catch (error) {
        throw new Error(`删除文章失败: ${error}`)
    }
}

// 更新文章meta信息
export async function updateArticleMeta(article: ArticleInter) {
    try {
        let data = await http.put("/articles/user/articleMeta", article) as ResultInter;
        return data
    } catch (error) {
        throw new Error(`更新文章失败: ${error}`)
    }
}

//  获取所有标签的接口
export async function getAlltagList() {
    try {
        let data = await http.get("/tags/user/tags") as ResultInter
        return data.data.list //直接返回了list的数据
    } catch (error) {

    }
}

// 删除标签，传来文章id 和标签名称
export async function deleteTagByArticleIdAndTagId(articleId: number, tagId: number) {
    try {
        let data: ResultInter = await http.delete(`/tags/user/tag?articleId=${articleId}&tagId=${tagId}`) as ResultInter;
        return data.message
    } catch (error) {

    }
}

// 为文章添加标签 传过去post请求 传过去一个article对象
export async function addTag(articleTag: ArticleTagInter) {
    try {
        let data: ResultInter = await http.post(`/tags/user/tag?articleId`, articleTag) as ResultInter;
        return data.message
    } catch (error) {

    }
}

// 编辑文章内容，获取文章的内容
export async function getUserArticleContentById(id:number) {
    try {
        let data = http.get(`articles/user/content/${id}`)
        return data;
    } catch (error) {

     }

}